<template>
  <div>
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
      <div style="margin:20px 0;">
        <span style="font-size:35px;font-weight:600;">{{topic}}编辑</span>
      </div>
      <div style="margin:10px 0;">
        <span style="font-size:25px;color:red;">(上下拖动简介、段落文本框右下角改变高度)</span>
      </div>
      <div style="margin:10px 0;">
        <span style="font-size:25px;color:red;">(段落换行直接回车换行即可，不需要考虑每段前空两个字符)</span>
      </div>
      <el-form-item prop="title" label="标题">
        <el-input style="font-size:18px;" v-model="dynamicValidateForm.title"></el-input>
      </el-form-item>
      <el-form-item v-show="showdesc" label="简介" :prop="dynamicValidateForm.introduction">
        <el-input style="font-size:18px;" type="textarea" v-model="dynamicValidateForm.introduction"></el-input>
      </el-form-item>
      <el-form-item label="内容" :prop="dynamicValidateForm.content">
        <el-input :autosize="{ minRows: 10}" style="font-size:18px;" type="textarea"
          v-model="dynamicValidateForm.content"></el-input>
      </el-form-item>

      <div style="margin:20px 0;" v-show="notice">
        <span style="font-size:35px;font-weight:600;">配图</span>
      </div>
      <div style="margin:10px 0;" v-show="notice">
        <span style="font-size:25px;color:red;">(已发布的文章支持修改文字部分，不支持修改图片，修改后点击提交)</span>
      </div>

      <div class="imgItem" v-show="notice">
        <div v-for="(item,index) in imgList" :key="index">
          <el-image class="img" fit="cover" :src="item.url">
          </el-image>
        </div>
      </div>
      <div style="margin:15px 0;">
        <el-button type="primary" @click="submitForm()">提交</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'News',
  data () {
    return {
      dynamicValidateForm: {
        id: this.id,
        title: '',
        introduction: '',
        content: ''
      },
      Form: {
        id: this.id,
        title: '',
        introduction: '',
        content: ''
      },
      imgList: [],
      id: '',
      name: '',
      showdesc: true,
      topic: '',
      notice: true
    }
  },
  methods: {
    submitForm (formName) {
      if (this.dynamicValidateForm.title && this.dynamicValidateForm.introduction && this.dynamicValidateForm.content) {
        this.Form = this.dynamicValidateForm
        var text = this.dynamicValidateForm.content
        var re = /\n/g
        var string = text.replace(re, '<span>')
        this.Form.content = string
        if (this.name === 'Case') {
          this.$axios({
            method: 'put',
            url: '/success/update',
            data: {
              'content': encodeURI(this.Form.content),
              'id': this.Form.id,
              'introduction': this.Form.introduction,
              'title': this.Form.title
            }
          }).then((res) => {
            if (res.status === 200) {
              this.open('修改成功！')
              this.getDeatil()
            }
          })
        } else if (this.name === 'News') {
          this.$axios({
            method: 'put',
            url: '/news/update',
            data: {
              'content': encodeURI(this.Form.content),
              'id': this.Form.id,
              'introduction': this.Form.introduction,
              'title': this.Form.title
            }
          }).then((res) => {
            if (res.status === 200) {
              this.open('修改成功！')
              this.getDeatil()
            }
          })
        } else if (this.name === 'Notice') {
          this.$axios({
            method: 'put',
            url: '/notice/update',
            data: {
              'content': encodeURI(this.Form.content),
              'id': this.Form.id,
              'introduction': this.Form.introduction,
              'title': this.Form.title
            }
          }).then((res) => {
            if (res.status === 200) {
              this.open('修改成功！')
              this.getDeatil()
            }
          })
        }
      }
    },
    open (text) {
      this.$notify({
        title: '成功',
        message: text,
        type: 'success'
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    changefile (file, fileList) {
      this.imgList = fileList
    },
    deleteimg (index) {
      this.imgList.splice(index, 1)
    },
    getDeatil () {
      this.name = this.$route.query.name
      this.id = this.$route.query.id
      if (this.name === 'Case') {
        this.topic = '案例'
        this.$axios({
          method: 'get',
          url: '/success/get?id=' + this.id
        }).then((res) => {
          if (res.status === 200) {
            var text = decodeURI(res.data.data.content)
            var data = res.data.data
            var re = /<span>/g
            var string = text.replace(re, '\n')
            data.content = string
            this.dynamicValidateForm = data
            this.imgList = res.data.data.successtList
          }
        })
      } else if (this.name === 'News') {
        this.showdesc = false
        this.topic = '新闻'
        this.$axios({
          method: 'get',
          url: '/news/get?id=' + this.id
        }).then((res) => {
          if (res.status === 200) {
            var text = decodeURI(res.data.data.content)
            var data = res.data.data
            var re = /<span>/g
            var string = text.replace(re, '\n')
            data.content = string
            this.dynamicValidateForm = data
            this.imgList = res.data.data.successtList
          }
        })
      } else if (this.name === 'Notice') {
        this.showdesc = false
        this.notice = false
        this.topic = '公告'
        this.$axios({
          method: 'get',
          url: '/notice/get?id=' + this.id
        }).then((res) => {
          if (res.status === 200) {
            var text = decodeURI(res.data.data.content)
            var data = res.data.data
            var re = /<span>/g
            var string = text.replace(re, '\n')
            data.content = string
            this.dynamicValidateForm = data
          }
        })
      }
    }
  },
  mounted () {
    this.getDeatil()
  }
}
</script>

<style scoped>
.imgItem {
  display: flex;
  justify-content: space-around;
  flex-wrap: wrap;
  overflow: hidden;
  margin: 15px 0;
}
.img {
  width: 450px;
  height: 300px;
}
.ipt {
  width: 350px;
}
</style>
